import os
import sys
import time

import logging

# 创建一个过滤器，用于过滤掉INFO级别的日志
class infoFilter(logging.Filter):
    def filter(self, record):
        return record.levelno == logging.INFO


# 创建一个过滤器，用于过滤掉err级别的日志
class errFilter(logging.Filter):
    def filter(self, record):
        return record.levelno == logging.ERROR

class logger:
    # 获取日志对象
    @classmethod
    def getLog(cls):
        # 创建一个日志记录器，并设置其级别为DEBUG
        cls.logger = logging.getLogger(__name__)
        cls.logger.setLevel(logging.DEBUG)
        # 创建一个文件处理程序，用于将日志写入文件
        '''2025-12-12.log'''
        '''2025-12-12-info.log'''
        '''2025-12-12-warning.log'''
        # 创建日志文件夹
        log_path = "./logs/"
        if not os.path.exists(log_path):
            os.mkdir(log_path)

        # 设置日志文件名称
        now = log_path + time.strftime('%Y-%m-%d')
        log_name =  '{}.log'.format(now)
        info_log_name = '{}.info'.format(now)
        err_log_name = '{}.err'.format(now)


        #创建文件处理器
        all_handler = logging.FileHandler(log_name, encoding='utf-8')
        info_handler = logging.FileHandler(info_log_name, encoding='utf-8')
        error_handler = logging.FileHandler(err_log_name, encoding='utf-8')
        # 设置日志级别
        all_handler.setLevel(logging.DEBUG)
        info_handler.setLevel(logging.INFO)
        error_handler.setLevel(logging.ERROR)

        # 创建处理器,将日志输出到控制台
        console = logging.StreamHandler()

        # 创建格式化程序
        formatter = logging.Formatter(
            "%(asctime)s %(levelname)s [%(name)s] [%(filename)s (%(funcName)s:%(lineno)d)] - %(message)s")
        all_handler.setFormatter(formatter)
        info_handler.setFormatter(formatter)
        error_handler.setFormatter(formatter)
        console.setFormatter(formatter)


        # 添加过滤器
        info_handler.addFilter(infoFilter())
        error_handler.addFilter(errFilter())


        # 添加到日志处理器中
        cls.logger.addHandler(all_handler)
        cls.logger.addHandler(info_handler)
        cls.logger.addHandler(error_handler)
        cls.logger.addHandler(console)
        return cls.logger